<?php
namespace app\admin\controller\order;

use think\Db;
class Driving extends \app\admin\controller\Common
{
    public function index(){
        $this->assign("order_status",(new \app\common\model\DrivingOrders())->order_status());
        return view();
    }

    public function list(){
        $limit = request()->param('limit') ?? 15;
        $page = request()->param('page');
        $page = $page ? ($page - 1) * $limit : 0;
        $search = request()->param();
    
        $query = (new \app\common\model\DrivingOrders())
                ->alias('order')
                ->join("{$this->tdb}agent agent","agent.id = order.agent_id")
                ->field([
                    'order.id','order.order_on','order.income_money','order.money','order.order_status',
                    'order.agent_id'
                ]);
        isset($search['agent_name']) && $search['agent_name'] != "" ? $query->where("agent.name like '%{$search['agent_name']}%'") : '';
        isset($search['order_status']) && $search['order_status'] != "" ? $query->where("order.order_status like '%{$search['order_status']}%'") : '';
        $query->order("order.id desc");
        $list = $query->limit($page, $limit)->append(['order_status_text','agent_name_text'])->select();
        $count = $query->count();
        return self::successResult('',$list,$count);
    }

    public function info()
    {
        if (request()->isPost()) {
            // $data = request()->param();
            // $validate = new \app\common\validate\Menu();
            // $result = $validate->check($data);
            // if ($result === false) {
            //     $this->error($validate->getError());
            // }
            // $table = new \app\common\model\Menu();
            // $row = $table->allowField(true)->save($data, ['id' => $data['id']]);
        
            // if ($row) {
            //     return self::successResult();
            // } else {
            //     return self::errorResult();
            // }
        } else {
            $id = intval(request()->param('id'));
            $info = (new \app\common\model\DrivingOrders())->where(['id' => $id])->field([])->append([
                'type_text','pay_status_text','pay_time_text','order_status_text',
                'rider_text','rider_status_text','refund_success_time_text','agent_name_text',
                'arrive_time_text','complete_time_text','rider_receiving_time_text',
                'pay_type_text','addtime_text','service_time_text','is_service_text','urgent_status_text',
                'is_weather_text'
                ])->find();
            $this->assign('info',$info);
            return view();
        }
    }

    public function delete(){
        $id = request()->param('id');
        if (empty($id)) $this->error("请选择要删除的数据");
        $table = new \app\common\model\Store();
        if (is_array($id)) {
            $id = implode(",", $id);
            $row = $table->whereIn('id', $id)->delete();
        } else {
            $row = $table->where('id', $id)->delete();
        }
         return $row ? self::successResult("成功") : self::errorResult("失败");
    }

    public function status(){
        $id = request()->param('id');
        $info = (new \app\common\model\StoreInformation())->alias('info')
                ->join("store store","store.id = info.store_id")
                ->field([
                    'info.id','store.title','info.full_name','info.store_id',
                    'info.full_id','info.full_id_image','info.storefront_image',
                    'info.registration','info.permit_image','info.restaurant'
                    ])
                ->where(['store_id' => $id])->find();
        if(empty($info)){
            return  "没有上传资料";
        }
        $this->assign('info',$info);
        return view();
    }
    public function certification(){
        $data = request()->param();
        if ($data['audit'] == 'yes') {
            (new \app\common\model\Store())->where('id', $data['id'])
                ->update(['status' => 2, 'status_fail' => '']);
        } else {
            (new \app\common\model\Store())->where('id', $data['id'])
                ->update(['status' => 3, 'status_fail' => $data['status_fail']]);
        }
        return self::successResult();
    }

  

 
}
